package com.zhj.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 力扣 1 两数之和
 * @author zhj
 */
public class Test1 {

    public static void main(String[] args) {
        int[] nums = {2,7,11,15};
        System.out.println(Arrays.toString(twoSum(nums, 9)));
    }

    public static int[] twoSum(int[] nums, int target) {
        if(nums == null || nums.length == 0) {
            return null;
        }
        Map<Integer, Integer> cache = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int val = target - nums[i];
            if (cache.containsKey(val)) {
                return new int[]{cache.get(val), i};
            }
            cache.put(nums[i], i);
        }
        return new int[0];
    }
}
